package org.jeecg.modules.xl.brand.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.xl.brand.entity.XlProductBrand;
import org.jeecg.modules.xl.brand.entity.XlProductTypebrand;
import org.jeecg.modules.xl.brand.service.IXlProductBrandService;
import org.jeecg.modules.xl.brand.service.XlProductTypebrandService;
import org.jeecg.modules.xl.cityconfig.entity.XlConfigCityDelivery;
import org.jeecg.modules.xl.producttype.service.IXlProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author deco
 * @create 2025-03-20
 */
@RestController
@RequestMapping("/slt/brand")
@Slf4j
public class XlBrandController extends JeecgController<XlProductBrand, IXlProductBrandService> {
    @Autowired
    private IXlProductBrandService brandService;
    @Autowired
    private XlProductTypebrandService xlProductTypebrandService;
    @GetMapping(value = "/list")
    public Result<IPage<XlProductBrand>> queryPageList(XlProductBrand xlProductBrand,
                                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                             HttpServletRequest req) {
        QueryWrapper<XlProductBrand> queryWrapper = QueryGenerator.initQueryWrapper(xlProductBrand, req.getParameterMap());
        Page<XlProductBrand> page = new Page<XlProductBrand>(pageNo, pageSize);
        IPage<XlProductBrand> pageList = brandService.page(page, queryWrapper);
        List<XlProductBrand> records = pageList.getRecords();
        //遍历 ，获取每个分类的L1code
        for(XlProductBrand record : records){
            QueryWrapper<XlProductTypebrand> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("brand_code",record.getBrandCode());
            List<XlProductTypebrand> types = xlProductTypebrandService.list(queryWrapper1);
            if(ObjectUtil.isNotEmpty(types)){
                String l1typename = types.stream().map(XlProductTypebrand::getL1Typename).collect(Collectors.joining(","));
                String l1typeCode = types.stream().map(XlProductTypebrand::getL1Typecode).collect(Collectors.joining(","));
                record.setL1typeName(l1typename);
                record.setL1Typecode(l1typeCode);
            }
        }
        return Result.OK(pageList);
    }

    @GetMapping(value = "/getL1Type")
    public Result<List<XlProductTypebrand>> getL1Type(
                                                @RequestParam(name="brandCode", defaultValue="1") Integer brandCode) {
        QueryWrapper<XlProductTypebrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_code",brandCode);
        return Result.OK(xlProductTypebrandService.list(queryWrapper));
    }

    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody XlProductBrand xlProductBrand) {
        brandService.save(xlProductBrand);
        List<String> L1TypeCodeList = Arrays.asList(xlProductBrand.getL1Typecode().split(","));
        for(String l1Code: L1TypeCodeList){
            XlProductTypebrand xlProductTypebrand =  new XlProductTypebrand();
            xlProductTypebrand.setL1Typecode(l1Code);
            xlProductTypebrand.setL1Typename(
                    productTypeService.getById(l1Code).getTypeDesc());
            xlProductTypebrand.setBrandCode(xlProductBrand.getBrandCode());
            xlProductTypebrand.setBrandName(xlProductBrand.getBrandName());
            xlProductTypebrandService.save(xlProductTypebrand);
        }
        return Result.OK("添加成功！");
    }
    @Autowired
    private  IXlProductTypeService productTypeService;
    @PostMapping(value = "/saveL1Code")
    public Result<String> saveL1Code(@RequestBody XlProductTypebrand xlProductBrand) {
       // xlProductBrandf =xlProductBrand.get()
        List<String> L1TypeCodeList = Arrays.asList(xlProductBrand.getL1Typecode().split(","));
        for(String l1Code: L1TypeCodeList){
            XlProductTypebrand xlProductTypebrand =  new XlProductTypebrand();
            xlProductTypebrand.setL1Typecode(l1Code);
            xlProductTypebrand.setL1Typename(
                    productTypeService.getById(l1Code).getTypeDesc());
            xlProductTypebrand.setBrandCode(xlProductBrand.getBrandCode());
            xlProductTypebrand.setBrandName(xlProductBrand.getBrandName());
            xlProductTypebrandService.save(xlProductTypebrand);
        }
        return Result.OK("添加成功！");
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestBody XlProductBrand xlProductBrand) {
        brandService.updateById(xlProductBrand);
        //先删除分类，再插入分类
        QueryWrapper<XlProductTypebrand> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("brand_code",xlProductBrand.getBrandCode());
        xlProductTypebrandService.remove(queryWrapper1);
        //然后再插入
        List<String> L1TypeCodeList = Arrays.asList(xlProductBrand.getL1Typecode().split(","));
        for(String l1Code: L1TypeCodeList){
            XlProductTypebrand xlProductTypebrand =  new XlProductTypebrand();
            xlProductTypebrand.setL1Typecode(l1Code);
            xlProductTypebrand.setL1Typename(
                    productTypeService.getById(l1Code).getTypeDesc());
            xlProductTypebrand.setBrandCode(xlProductBrand.getBrandCode());
            xlProductTypebrand.setBrandName(xlProductBrand.getBrandName());
            xlProductTypebrandService.save(xlProductTypebrand);
        }
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */

    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        brandService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */

    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.brandService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @GetMapping(value = "/queryById")
    public Result<XlProductBrand> queryById(@RequestParam(name="id",required=true) String id) {
        XlProductBrand xlConfigCityDelivery = brandService.getById(id);
        if(xlConfigCityDelivery==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(xlConfigCityDelivery);
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, XlProductBrand xlConfigCityDelivery) {
        return super.exportXls(request, xlConfigCityDelivery, XlProductBrand.class, "XlProductBrand");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, XlProductBrand.class);
    }
}
